package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Petinfo;
import com.pojo.Raising;
import com.pojo.User;
import com.service.impl.PetInfoServiceimpl;
import com.service.impl.UserServiceimpl;
import com.util.UploadImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Zhengjz
 * @version 1.0
 * @className PetController
 * @date 2021/3/1 13:29
 */
@Controller
public class PetController {
    @Autowired
    private PetInfoServiceimpl petInfoServiceimpl;

    @Autowired
    private UserServiceimpl userServiceimpl;

//跳转宠物添加页面
    @RequestMapping("/addpetinfo")
    public String addPetInfo(){
        return "pet/addstudent";
    }



//    //上传宠物信息
//    @PostMapping("/petadd")
//    public void petAdd(Petinfo petinfo, @RequestParam(value = "file", required = false) MultipartFile upload, HttpServletResponse response,HttpServletRequest request) throws IOException {
//        //上传文件的位置,默认会在项目根目录找static文件夹,需手动创建,不然找到是临时路径。
//        String path = request.getSession().getServletContext().getRealPath("/uploads/");
//        System.out.println("路径："+path);
//        //判断该路径是否存在
//        File file = new File(path);
//        // 若不存在则创建该文件夹
//        if(!file.exists()){
//            file.mkdirs();
//        }
//        //获取上传文件的后缀名
//        String fileType  =upload.getOriginalFilename().substring(upload.getOriginalFilename().indexOf("."));
//        // 获取上传文件的名称
//       // String filenamee = upload.getOriginalFilename();
//        // 把文件的名称设置唯一值，uuid
//        String uuid = UUID.randomUUID().toString().replace("-", "");
//        String filename = path+uuid+fileType;
//        // todo 把文件名存入数据库(此步骤省略)
//
//        // 完成文件上传
//        try {
//            upload.transferTo(new File(path,filename));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        User user = (User) request.getSession().getAttribute("user");
//        User byId = userServiceimpl.findById(user.getUserName());
//        Petinfo petinfo1 = new UploadImg().uploadImg(filename, petinfo,byId.getId());
//        petInfoServiceimpl.addPet(petinfo);
//
//
//        response.getWriter().println("添加成功！");
//
//    }
//




    //上传宠物信息
    @PostMapping("/petadd")
    public void petAdd(Petinfo petinfo, @RequestParam(value = "file", required = false) MultipartFile file, HttpServletResponse response,HttpServletRequest request) throws IOException {
        System.out.println("宠物信息："+petinfo);
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());
        Petinfo petinfo1 = new UploadImg().uploadImg(file, petinfo,byId.getId());
        petInfoServiceimpl.addPet(petinfo);


        response.getWriter().println("添加成功！");

    }


    //宠物信息的展示
    @RequestMapping("/petinfolist")
    public String petInfoList(HttpServletRequest request, Integer pageNum, Model model){
        if (pageNum == null) {
            pageNum = 1;
        }
        String img = "D:\\图片\\46eb5418-a767-41b0-baa1-227d6ad1ef53.jpg";
        String name = request.getParameter("name");
        if (name == null || name == "") {
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            List<Petinfo> all = petInfoServiceimpl.findAll();
            PageInfo pageTeacher = new PageInfo<>(all);
            model.addAttribute("list", pageTeacher);
            model.addAttribute("img",img);
        }else{
            model.addAttribute("name",name);
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            List<Petinfo> byName = petInfoServiceimpl.findByName(name);
            PageInfo pageTeacher =  new PageInfo<>(byName);
            model.addAttribute("list",pageTeacher);
        }

        return "pet/allstudent";
    }


    //删除宠物信息
    @RequestMapping("/delpetid")
    public void delPetId(String id,HttpServletResponse response) throws IOException {
        System.out.println("id:"+id);
        int i = petInfoServiceimpl.delById(id);
        response.getWriter().println();
    }



    //我的宠物信息
    @RequestMapping("/ipet")
    public String iPet(HttpServletRequest request,Integer pageNum){
        if (pageNum == null) {
            pageNum = 1;
        }
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());
        Integer id = byId.getId();
        String name = request.getParameter("name");
        if(name == null || name == ""){

            System.out.println("宠物id:"+id);
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            List<Petinfo> petinfos = petInfoServiceimpl.iPet(id);
            PageInfo pageTeacher =  new PageInfo<>(petinfos);
            System.out.println("宠物信息："+petinfos);
            request.setAttribute("list",pageTeacher);
        }else{
            request.setAttribute("name",name);
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            Map map = new HashMap();
            map.put("name",name);
            map.put("id",id);
            List<Petinfo> petinfos = petInfoServiceimpl.iPetNamt(map);
            PageInfo pageTeacher =  new PageInfo<>(petinfos);
            request.setAttribute("list",pageTeacher);
        }

        return "pet/ipet";
    }


}
